package rigidbody;

/**
 * @author Jeran Fox
 * 
 * 2010-12-1
 */

import geometry.ObjModel;

import javax.vecmath.Point3d;

public class ObjModelRigidBody extends RigidBody {
	
	public ObjModelRigidBody(double mass_, Point3d position_, String fn_)
    {
        this(mass_, position_, new ObjModel(fn_));
    }
	
	public ObjModelRigidBody(double mass_, Point3d position_, ObjModel objModel_)
    {
        super(mass_, position_, objModel_);
        initASDF();
        
        //approximate inertia with n point masses of even mass where n
        //is the number of vertices in the triangle mesh
        for (Point3d v : m_geometry.getTriangleMesh()._vertices)
        {
        	m_initialInertia.m00 += v.y * v.y + v.z * v.z;
            m_initialInertia.m11 += v.x * v.x + v.z * v.z;
            m_initialInertia.m22 += v.x * v.x + v.y * v.y;
            m_initialInertia.m01 -= v.x * v.y;
            m_initialInertia.m02 -= v.x * v.z;
            m_initialInertia.m12 -= v.y * v.z;
        }
        m_initialInertia.m10 = m_initialInertia.m01;
        m_initialInertia.m20 = m_initialInertia.m02;
        m_initialInertia.m21 = m_initialInertia.m12;
        
        m_initialInertia.mul(mass_ / m_geometry.getTriangleMesh()._vertices.size());
    }
}
